package com.bcxj.servicedriveruser.servcie;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bcxj.internalcommon.constant.CommonStatusEnum;
import com.bcxj.internalcommon.constant.DriverCarConstants;
import com.bcxj.internalcommon.dto.ResponseResult;
import com.bcxj.internalcommon.entity.DriverCarBindingRelationship;
import com.bcxj.servicedriveruser.mapper.DriverCarBindingRelationshipMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * ClassName: DriverCarBindingRelationshipService
 * Package: com.bcxj.servicedriveruser.servcie
 * Description:
 *
 * @Author BCXJ
 * @Create 2024/10/20 11:15
 * @Version 1.0
 * @Since 1.0
 */
@Service
public class DriverCarBindingRelationshipService {

    @Autowired
    private DriverCarBindingRelationshipMapper driverCarBindingRelationshipMapper;

    /**
     * 车辆与司机的绑定
     * @param driverCarBindingRelationship
     * @return
     */
    public ResponseResult bind(DriverCarBindingRelationship driverCarBindingRelationship) {
        // 查看车辆与司机是否绑定
        LambdaQueryWrapper<DriverCarBindingRelationship> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DriverCarBindingRelationship::getDriverId, driverCarBindingRelationship.getDriverId())
                .eq(DriverCarBindingRelationship::getCarId, driverCarBindingRelationship.getCarId())
                .eq(DriverCarBindingRelationship::getBindState, DriverCarConstants.DRIVER_CAR_BIND);

        Integer count = driverCarBindingRelationshipMapper.selectCount(queryWrapper);
        if(count > 0) {
            return ResponseResult.fail(CommonStatusEnum.DRIVER_CAR_BING_EXIST);
        }

        // 司机是否已经绑定车辆
        queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DriverCarBindingRelationship::getDriverId, driverCarBindingRelationship.getDriverId())
                .eq(DriverCarBindingRelationship::getBindState, DriverCarConstants.DRIVER_CAR_BIND);
        count = driverCarBindingRelationshipMapper.selectCount(queryWrapper);
        if(count > 0) {
            return ResponseResult.fail(CommonStatusEnum.DRIVER_BIND_EXIST);
        }

        // 车辆是否已经被绑定
        queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DriverCarBindingRelationship::getCarId, driverCarBindingRelationship.getCarId())
                .eq(DriverCarBindingRelationship::getBindState, DriverCarConstants.DRIVER_CAR_BIND);
        count = driverCarBindingRelationshipMapper.selectCount(queryWrapper);

        if(count > 0) {
            return ResponseResult.fail(CommonStatusEnum.CAR_BIND_EXIST);
        }

        // 补充时间日期的信息
        driverCarBindingRelationship.setBindingTime(LocalDateTime.now());
        driverCarBindingRelationship.setBindState(DriverCarConstants.DRIVER_CAR_BIND);

        // 插入数据
        driverCarBindingRelationshipMapper.insert(driverCarBindingRelationship);

        return ResponseResult.success();
    }


    /**
     * 司机与车辆解绑
     * @param driverCarBindingRelationship
     * @return
     */
    public ResponseResult unBind(DriverCarBindingRelationship driverCarBindingRelationship) {
        LambdaQueryWrapper<DriverCarBindingRelationship> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DriverCarBindingRelationship::getDriverId, driverCarBindingRelationship.getDriverId())
                .eq(DriverCarBindingRelationship::getCarId, driverCarBindingRelationship.getCarId())
                .eq(DriverCarBindingRelationship::getBindState, DriverCarConstants.DRIVER_CAR_BIND);

        DriverCarBindingRelationship queryObj = driverCarBindingRelationshipMapper.selectOne(queryWrapper);

        // 关系不存在
        if(null == queryObj) {
            return ResponseResult.fail(CommonStatusEnum.DRIVER_CAR_BING_NOT_EXIST);
        }

        // 补充修改信息
        queryObj.setUnBindingTime(LocalDateTime.now());
        queryObj.setBindState(DriverCarConstants.DRIVER_CAR_UNBIND);

        driverCarBindingRelationshipMapper.updateById(queryObj);
        return ResponseResult.success();
    }
}
